package com.gmall.console.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmall.console.base.Result;
import com.gmall.console.system.entity.Role;
import com.gmall.console.system.service.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jone
 * @since 2024-05-29
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    IRoleService iRoleService;

    @PostMapping("/add")
    @Operation(summary = "新增接口", description = "新增接口描述")
    public Result<Map<String,Object>> add(@RequestBody Role role){
        role.setCreateTime(LocalDateTime.now());
        iRoleService.addRole(role);
        return Result.success("数据添加成功");
    }

    @PutMapping("/update")
    @Operation(summary = "修改接口", description = "修改描述")
    public Result<Map<String,Object>> update(@RequestBody Role role){
        role.setUpdateTime(LocalDateTime.now());
        iRoleService.updateRole(role);
        return Result.success("修改成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取信息", description = "获取信息")
    public Result<Role> getRoleInfo(@PathVariable("id") String id){
        Role role = iRoleService.getRoleInfo(id);
        return Result.success(role);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除信息", description = "删除信息")
    public Result<Role> deleteRole(@PathVariable("id") String id){
        iRoleService.removeById(id);
        return Result.success("删除成功！");
    }

    @GetMapping("/list")
    @Operation(summary = "列表接口", description = "列表接口描述")
    public Result<Map<String,Object>> list(@RequestParam(value = "roleName",required = false) String roleName,
                                           @RequestParam("pageNo") int pageNo,
                                           @RequestParam("pageSize") int pageSize) {

        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(roleName),Role::getRoleName,roleName);
        wrapper.orderByDesc(Role::getId);

        Page<Role> page = new Page(pageNo,pageSize);

        iRoleService.page(page, wrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data,"查询成功");
    }

    @GetMapping("/all")
    @Operation(summary = "所有列表接口", description = "所有列表接口描述")
    public Result<List<Role>> all() {

        List<Role> list = iRoleService.list();

        return Result.success(list,"查询成功");
    }

}
